package com.zllkj.mm.module.goods

import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alipay.sdk.app.PayTask
import com.zllkj.mm.R
import com.zllkj.mm.adapter.CollectionListAdapter
import com.zllkj.mm.adapter.NearByShopGoodsListAdapter
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseFragment
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.common.L
import com.zllkj.mm.model.entity.AddShoppingCartVo
import com.zllkj.mm.model.entity.NearByGoodsListBean
import com.zllkj.mm.module.goods.vm.ItemCollectionsVM
import com.zllkj.mm.module.goods.vm.ItemNearByShopGoodsVM
import com.zllkj.mm.module.shopping.fragment.OrderListItemFragment
import com.zllkj.mm.module.shopping.fragment.ShopDetailFragment
import com.zllkj.mm.util.StaggeredDividerItemDecoration
import com.zllkj.mm.util.common.FormatUtil
import kotlinx.android.synthetic.main.fragment_mycollection.view.*
import kotlinx.android.synthetic.main.fragment_order_list.*
import java.util.HashMap

class NearByGoodsListItemFragment(val type:Int, val goodsKeyWord:String, val lat:Double, val lon:Double) : BaseFragment() {

    var currentPage = 1


    var datas = ArrayList<ItemNearByShopGoodsVM>()
    lateinit var adapter : NearByShopGoodsListAdapter
    override fun getLayoutId(): Int {
        return R.layout.fragment_mycollection
    }


    override fun loadData() {
        super.loadData()

        adapter = NearByShopGoodsListAdapter(R.layout.item_near_by_shop_goods_list,datas)
        adapter.setHasStableIds(true)
        adapter.setOnItemClickListener { baseQuickAdapter, view, i ->
            addFragment(GoodsDetailFragment(datas[i].goodsId))

        }

        adapter.setOnItemChildClickListener { baseQuickAdapter, view, i ->
            when(view.id) {
                R.id.tv_near_by_shop_add_to_shoppingcart -> {

                    addSubscribe(RetrofitHelper.getInstance().shoppingCartApi.addGoodsToShoppingCart((HashMap<String, Any>().apply {
                        this["goodsId"] = datas[i].goodsId
                        this["goodsNum"] = 1
                    })).compose<AddShoppingCartVo>(RxHelper.defaultHandleResult<AddShoppingCartVo>()).subscribe(object : ProgressSubscriber<AddShoppingCartVo>(requireContext()) {
                        override fun onSucc(t: AddShoppingCartVo?) {
                           L.showToast("添加成功")
                        }

                        override fun onError(e: Throwable?) {
                            super.onError(e)
                        }

                    }))
                }
                R.id.tv_near_by_shop_goods_shopname -> {
                    addFragment(ShopDetailFragment(datas[i].shopId))
                }
            }

        }

        mRootView.smartRefreshlayout_mycollection?.apply {
            setOnRefreshListener {
                datas.clear()
                currentPage = 1
                loadListData(goodsKeyWord,OrderListItemFragment.PULL_DOWN)
            }

            setOnLoadMoreListener {
                currentPage += 1
                loadListData(goodsKeyWord,OrderListItemFragment.LOAD_MORE)
            }
        }
        mRootView.rv_mycollection.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        mRootView.rv_mycollection.addItemDecoration(StaggeredDividerItemDecoration(requireContext(),8f,8))
        mRootView.rv_mycollection.adapter = adapter
        loadListData("",-1)
    }


    fun loadListData(keyWord:String,loadStatus : Int){
        if(type == 0) {
            addSubscribe(RetrofitHelper.getInstance().goodsApi.getNearByGoodsList((HashMap<String, Any>().apply {
                this["goodsName"] = keyWord
                this["latitude"] = lat
                this["longitude"] = lon
                this["pageSize"] = 20
                this["currentPage"] = currentPage
                this["sort"] = 0
            })).compose<NearByGoodsListBean>(RxHelper.defaultHandleResult<NearByGoodsListBean>()).subscribe(object : ProgressSubscriber<NearByGoodsListBean>(requireContext()) {
                override fun onSucc(t: NearByGoodsListBean?) {

                    t?.records?.let {
                        it.map {
                            var vm = ItemNearByShopGoodsVM()
                            vm.unitPrice = FormatUtil.bigDecimalUtilFormat(it.unitPrice.toString(),"100")
                            vm.shopName = it.shopName
                            vm.goodsName = it.goodsName
                            vm.saleCount = it.sales.toString()
                            vm.picUrl = it.goodsPic
                            vm.shopId = it.shopId.toString()
                            vm.goodsId = it.goodsId.toString()
                            vm.isAd = it.advertising == 1
                            datas.add(vm)
                        }
                        if(loadStatus == OrderListItemFragment.PULL_DOWN) {
                            mRootView.smartRefreshlayout_mycollection?.finishRefresh()
                            adapter.setNewData(datas)
                        } else if (loadStatus == OrderListItemFragment.LOAD_MORE) {
                            mRootView.smartRefreshlayout_mycollection?.finishLoadMore()
                            adapter.notifyDataSetChanged()
                        } else {
                            adapter.notifyDataSetChanged()
                        }

                    }


                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                    Log.d("acthome","error" )
                }

            }))
        } else {
            addSubscribe(RetrofitHelper.getInstance().goodsApi.getNearByFocusGoodsList((HashMap<String, Any>().apply {
                this["goodsName"] = ""
                this["pageSize"] = 20
                this["currentPage"] = currentPage
                this["sort"] = 0
            })).compose<NearByGoodsListBean>(RxHelper.defaultHandleResult<NearByGoodsListBean>()).subscribe(object : ProgressSubscriber<NearByGoodsListBean>(requireContext()) {
                override fun onSucc(t: NearByGoodsListBean?) {
                    t?.records?.let {
                        it.map {
                            var vm = ItemNearByShopGoodsVM()
                            vm.unitPrice = FormatUtil.bigDecimalUtilFormat(it.unitPrice.toString(),"100")
                            vm.shopName = it.shopName
                            vm.saleCount = it.sales.toString()
                            vm.goodsName = it.goodsName
                            vm.picUrl = it.goodsPic
                            vm.goodsId = it.goodsId.toString()
                            datas.add(vm)

                        }
                    }

                    if(loadStatus == OrderListItemFragment.PULL_DOWN) {
                        mRootView.smartRefreshlayout_mycollection?.finishRefresh()
                        mRootView.rv_mycollection.layoutManager = GridLayoutManager(requireContext(),2)
                        mRootView.rv_mycollection.adapter = adapter
                    } else if (loadStatus == OrderListItemFragment.LOAD_MORE) {
                        mRootView.smartRefreshlayout_mycollection?.finishLoadMore()
                        adapter.setNewData(datas)
                    } else {
                        adapter.setNewData(datas)
                    }
                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                }

            }))
        }
    }

}